 package com.tnl.utils;
 
 import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
 public class DateUtil
 {
   public static String defaultTimePattern = "yyyy-MM-dd HH:mm:ss";
   public static String defaultTimePattern1 = "yyyyMMddHHmmss";
   public static String getDateTimeStr(long time)
   {
     java.sql.Date date = new java.sql.Date(time * 1000L);
     SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     
     String str = bartDateFormat.format(date);
     return str;
   }
   
	/**
	 * 获得表示 当前时间 的字符串 格式:"yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public static String currentTime() {
		return new SimpleDateFormat(defaultTimePattern).format(System.currentTimeMillis());
	}
	/**
	 * 获得表示 当前时间 的字符串 格式:"yyyyMMddHHmmss"
	 * @return
	 */
	public static String currentTimeStr(){
		return new SimpleDateFormat(defaultTimePattern1).format(System.currentTimeMillis());
	}
	
   public static String getDateStr(long time)
   {
     java.sql.Date date = new java.sql.Date(time);
     SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
     
     String str = bartDateFormat.format(date);
     return str;
   }
   
   public static String getDateTimeStr(java.util.Date date)
   {
     SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     
     String str = bartDateFormat.format(date);
     return str;
   }
   
   public static java.util.Date toDate(String dateStr)
   {
     if ((dateStr == null) || ("".equals(dateStr))) {
       return null;
     }
     java.util.Date date = null;
     SimpleDateFormat formater = new SimpleDateFormat();
     formater.applyPattern("yyyy-MM-dd");
     try
     {
       date = formater.parse(dateStr);
     }
     catch (ParseException e)
     {
       e.printStackTrace();
     }
     return date;
   }
   
   public static long toTimeStamp(String tsStr)
   {
     Timestamp ts = new Timestamp(System.currentTimeMillis());
     try
     {
       ts = Timestamp.valueOf(tsStr);
     }
     catch (Exception e)
     {
       e.printStackTrace();
     }
     return ts.getTime();
   }
   
   public static String timestamp2String(Timestamp ts)
   {
     String tsStr = "";
     DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
     try
     {
       tsStr = sdf.format(ts);
     }
     catch (Exception e)
     {
       e.printStackTrace();
     }
     return tsStr;
   }
   

   public static String getFormatTime(Date date, String format) {
       SimpleDateFormat sdf = new SimpleDateFormat(format);
       return sdf.format(date);
   }

   public static Date addDay(Date date, int day) {
       Calendar cal = Calendar.getInstance();
       cal.setTime(date);
       cal.add(Calendar.DATE, 2);
       return cal.getTime();
   }

   public static Date minusDay(Date date, int day) {
       return addDay(date, -day);
   }
   

	
   public static Date dateAddDay(int num,Date date){
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.DATE, num);
        return ca.getTime();
    }
	
	/**
	 * 获取当前日期是星期几<br>
	 * 
	 * @param date
	 * @return 当前日期是星期几
	 */
	public static int getWeekOfDate(Date date) {
		//String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return w;
	}
	/**
	 * 时间比较大小
	 * @param DATE1
	 * @param DATE2
	 * @return
	 */
	public static int compareDate(String DATE1, String DATE2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * get current month
	 * @return
	 */
	public static int getMonth(){
		Calendar cal=Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		int month=cal.get(Calendar.MONTH)+1;
		return month;
	}
	
	/**
	 * get current day
	 * @return
	 */
	public static int getDay(){
		Calendar cal=Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		int day = cal.get(Calendar.DATE);
		return day;
	}
	
	public static void main(String[] args) {
		String time="2019-05-25  16:00-16:50";
		System.out.println(time.substring(8, 10));
	}
 }


